<?php
	/**
	 * 手机端管理员操作类
	 * @author leiqianyong 2016-12-19 13:54 PM
	 */
	class manager extends core{

		//管理员信息表
		private $table_admin = 'syt_admin';
		//项目
		private $table_project = 'syt_project';
		//设备绑定信息
		private $table_bind = 'syt_bind';
		//设备信息
		private $table_device= 'syt_device';
		//记录表
		private $table_record= 'syt_checkin';


		//===== 内部方法，需要登录才能操作  ===================================================================

		/**
		 * 获取当前管理员的账号信息
		 * @author leiqianyong 2016-12-19 13:56 PM
		 */
		public function admin_info($cols='id,account'){
            $id=is_cookie_login();
			//检查参数
			if(!$id)return false;

			//查询信息
			$sql = "SELECT {$cols} FROM {$this->table_admin} WHERE `id`={$id}";
			$row = $this->_mysql(0)->findone($sql);
			if(!$row)return false;

			return $row;
		}


		/**
		 * 通过包含经理id的数组查找详情信息
		 * @param array $data
		 */
		public function admin_rtn($data,$field='uid',$cols='id,name'){
			if(!is_array($data))return false;

			foreach($data as $val){
				$dary[]=$val[$field];
			}
			$dary=array_unique($dary);

			$dis=implode(',', $dary);
			$sql="select {$cols} from {$this->table_admin} where id in ({$dis})";

			$res=$this->_mysql(0)->find($sql);
			if(!$res)return false;

			foreach($res as $vo){
			$rtn[$vo['id']]=$vo;
			}

			return $rtn;
		}


		/**
		 * 查看项目经理信息
		 * @param int $id
		 * @param string $cols
		 */
		public function projecter_info($id,$cols='*'){
			if(!is_numeric($id))return false;

			$sql="select {$cols} from {$this->table_admin} where id={$id}";

			return $this->_mysql(0)->findone($sql);
		}


		/**
		 * 当前项目账号对应的项目经理id
		 * @author leiqianyong 2016-12-21 9:42 AM
		 */
		public function admin_project_id(){
			$id=is_cookie_login();
			//检查参数
			if(!$id)return false;

			$sql="select id,master from {$this->table_admin} where id={$id}";
			$row = $this->_mysql(0)->findone($sql);
			if(!$row)return false;

			return $row['master']?$row['master']:$row['id'];
		}


		/**
		 * 查看项目经理的项目总数
		 * @param int $id 项目经理id
		 */
		public function project_count($id){
			if(!is_numeric($id))return false;

			$sql="select count(`id`) AS num from {$this->table_project} where 1";

			if(!is_admin($id)){
				$sql.=" AND uid={$id}";
			}

			$res=$this->_mysql(0)->findone($sql);
			return $res['num']>0?$res['num']:0;
		}


		/**
		 * 查看项目列表
		 * @param int $id 项目经理id
		 */
		public function project_search($id,$type=1){
			if(!is_numeric($id))return false;

			$sql="select * from {$this->table_project} where 1 ";

			if(!is_admin($id)){
				$sql.=" AND uid={$id}";
			}

			$sql .=" order by id desc";

			$res=$this->_mysql(0)->find($sql);
			if(empty($res))return false;

			//按状态返回项目数据
			if(1==$type){
				foreach($res as $val){
					$result[$val['state']][]=$val;
				}
			}

			if(2==$type){
				foreach($res as $val){
					$result[$val['id']]=$val['title'];
				}
			}

			return $result;
		}


		/**
		 * 查看项目信息
		 * @param int $id
		 * @param string $cols
		 */
		public function project_info($id,$cols='*'){
			if(!is_numeric($id))return false;

			$sql="select {$cols} from {$this->table_project} where id={$id}";

			return $this->_mysql(0)->findone($sql);
		}


		/**
		 * 查看项目经理绑定的设备总数
		 * @param int $id
		 */
		public function device_count($id){
			if(!is_numeric($id))return false;

			//总管理
			if(is_admin($id)){
				$sql="select count(`id`) AS num from {$this->table_bind}";
				$re=$this->_mysql(0)->findone($sql);
				return $re['num']>0?$re['num']:0;
			}

			$sql="SELECT GROUP_CONCAT(id) AS pids from {$this->table_project} WHERE uid={$id}";
			$res=$this->_mysql(0)->findone($sql);
			if(empty($res))return 0;

			$sql="select count(`id`) AS num from {$this->table_bind} where pid in ({$res['pids']})";
			$re=$this->_mysql(0)->findone($sql);
			return $re['num']>0?$re['num']:0;
		}


		/**
		 * 查看项目列表
		 * @param int $id 项目经理id
		 * @param int $page
		 */
		public function device_search($cond,$page=1,$pagesize=20){
			$id=$cond['pid'];
			if(!is_numeric($id))return false;

			$sql="SELECT GROUP_CONCAT(id) AS pids from {$this->table_project} WHERE 1";

			if(!is_admin($id)){
				$sql.=" AND uid={$id}";
			}

			if($cond['proid']){
				$sql.=" AND id={$cond['proid']}";
			}

			$res=$this->_mysql(0)->findone($sql);
			if(empty($res))return 0;

			$sql="select * from {$this->table_bind} where pid in ({$res['pids']}) order by car_number asc";

			return $this->_mysql(0)->findlimit($sql,$page,$pagesize);
		}


		//查看绑定信息
		public function bind_info($id,$cols='*'){
			if(!is_numeric($id))return false;

			$sql="select {$cols} from {$this->table_bind} where did={$id}";

			return $this->_mysql(0)->findone($sql);
		}


		/**
		 * 通过包含设备id的数组查找设备信息
		 * @param array $data
		 */
		public function device_rtn($data){
			if(!is_array($data))return false;

			foreach($data as $val){
				$dary[]=$val['did'];
			}

			$dis=implode(',', $dary);
			$sql="select id,no from {$this->table_device} where id in ({$dis})";
			$res=$this->_mysql(0)->find($sql);
			if(!$res)return false;

			foreach($res as $vo){
				$rtn[$vo['id']]=$vo['no'];
			}

			return $rtn;
		}


		/**
		 * 查看登记记录
		 * @param int $id 项目经理id
		 * @param int $page
		 */
		public function record_search($cond,$page=1,$pagesize=20,$cols='*'){
			$id=$cond['pid'];
			if(!is_numeric($id))return false;

			$sql="select {$cols} from {$this->table_record} where state=1";

			if(!is_admin($id)){
				$sql.=" and xid={$id}";
			}

			if($cond['proid']){
				$sql.=" AND pid={$cond['proid']}";
			}

			if($cond['type']){
				$sql.=" AND type={$cond['type']}";
			}

			if($cond['did']){
				$sql.=" AND did={$cond['did']}";
			}

			if($cond['isdate']){
			    if($cond['date']) {
                    $starttime = strtotime($cond['date']);
                    $sql.=" AND ctime>={$starttime}";
                }

			    if($cond['date_end']){
                    $endtime = strtotime($cond['date_end']);
                    $sql.=" AND ctime<{$endtime}";
                }
            }

			if($cond['type']){
				$sql .= " ORDER BY dname ASC,ctime DESC";
			}else{
				$sql.=" order by ctime desc";
			}

			return $this->_mysql(0)->findlimit($sql,$page,$pagesize);
		}


		/**
		 * 记录详情
		 * @param int $id   记录id
		 * @param int $pid  项目经理id
		 * @param unknown $cols
		 */
		public function record_info($id,$pid=0,$cols='*'){
			if(!is_numeric($id) || !is_numeric($pid))return false;

			$sql="select {$cols} from {$this->table_record} where id={$id}";

			if($pid>0 && !is_admin($pid)){
				$sql.=" and xid={$pid}";
			}

			return $this->_mysql(0)->findone($sql);
		}


		/**
		 * 根据条件查询计时的登记列表
		 *
		 * @param mixed $data
		 * @param int $page
		 * @param int $pagesize
		 */
		public function record_time_search($cond,$page=1,$pagesize=10){
			if((!is_numeric($page) || !is_numeric($pagesize)) && !$pagesize===false){
				return false;
			}
			$sql = "SELECT id,dname,downer,ctime,time,dno,img,delimg,count(id) AS ti FROM {$this->table_record} WHERE type=2 AND state=1 " ;

			if(!is_admin($cond['pid'])){
			   $sql.=" AND xid={$cond['pid']}";
			}

			//根据工程的id查询
			if($cond['proid']){
				$sql .= " AND pid='{$cond['proid']}'";//必须选择项目
			}

			//根据设备号
			if($cond['did']){
				$sql .= " AND did={$cond['did']}";
			}

			if(strlen($cond['date'])>8){
		         $starttime=strtotime($cond['date']);
		         $endtime=$starttime+86400;

		         $sql.=" AND ctime>={$starttime} AND ctime<{$endtime}";
			}

			$sql .= " GROUP BY pid,did,`group` ORDER BY ctime DESC";

			return $this->_mysql(0)->findlimit($sql,$page,$pagesize,'group');
		}


        /**
         * 根据条件查询计时的全部登记列表
         *
         * @param mixed $data
         * @param int $page
         * @param int $pagesize
         */
        public function record_time_search_all($cond,$page=1,$pagesize=10){
            if((!is_numeric($page) || !is_numeric($pagesize)) && !$pagesize===false){
                return false;
            }
            $sql = "SELECT id,dname,downer,ctime,time,dno,img,delimg,jsno,dkno FROM {$this->table_record} WHERE type=2 AND state=1 " ;

            if(!is_admin($cond['pid'])){
                $sql.=" AND xid={$cond['pid']}";
            }

            //根据工程的id查询
            if($cond['proid']){
                $sql .= " AND pid='{$cond['proid']}'";//必须选择项目
            }

            //根据设备号
            if($cond['did']){
                $sql .= " AND did={$cond['did']}";
            }

            if($cond['isdate']){
                if($cond['date']) {
                    $starttime = strtotime($cond['date']);
                    $sql.=" AND ctime>={$starttime}";
                }

                if($cond['date_end']){
                    $endtime = strtotime($cond['date_end']);
                    $sql.=" AND ctime<{$endtime}";
                }
            }

            $sql .= " ORDER BY did ASC,jsno ASC,dkno ASC,ctime desc";

            return $this->_mysql(0)->findlimit($sql,$page,$pagesize);
        }


        /**
         * 查看会员的一次计时详情
         * @param int $pid
         * @param int $did
         * @param int $group
         * @param string $cols
         */
		public function record_time_group($pid,$did,$group,$cols='*'){
			$sql = "SELECT {$cols} FROM {$this->table_record} WHERE type=2 AND state=1 AND pid={$pid} AND did={$did} AND `group`={$group}" ;
			return $this->_mysql(0)->find($sql);
		}


		//===== 外部方法，操作不需要登录  ===================================================================




		/**
		 * 修改管理员信息
		 * @param int $id
		 * @param array $data
		 */
		public function admin_edit($id,$data){
			if(!is_numeric($id))return false;

			$where="id={$id}";
			$this->set_db($this->table_admin, $data,false,$where);
			return true;
		}


	}
?>
